package edu.nuc.event.controller;

import edu.nuc.event.pojo.Competition;
import edu.nuc.event.pojo.Place;
import edu.nuc.event.pojo.User;
import edu.nuc.event.service.ComService;
import edu.nuc.event.service.PlaceService;
import edu.nuc.event.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class ComController {

    @Autowired
    private ComService comService;

    @Autowired
    private UserService userService;

    @Autowired
    private PlaceService placeService;


    @GetMapping("/todetails1")
    public String todetails1(Model model) {
        List<Competition> coms = comService.findAllComs();

        int comcount = comService.allComsaccount();


        model.addAttribute("coms", coms);
        model.addAttribute("comcount", comcount);

        return "event-details1";
    }

    @GetMapping("/todetails2")
    public String todetails2(Model model) {
        List<Competition> coms = comService.findAllComs();

        int comcount = comService.allComsaccount();


        model.addAttribute("coms", coms);
        model.addAttribute("comcount", comcount);

        return "event-details2";
    }

    @GetMapping("/todetails3")
    public String todetails3(Model model, HttpSession session) {
        User user = (User) session.getAttribute("loginuser");
        if (user.getCpid() == 0) {
            return "redirect:/noplay";
        } else {
            Competition competition = comService.selectComByid(user.getCpid());

            model.addAttribute("coms", competition);

            return "event-details3";
        }

    }

    @GetMapping("/todetails5")
    public String todetails5(Model model) {
        List<Competition> coms = comService.findAllComs();

        int comcount = comService.allComsaccount();


        model.addAttribute("coms", coms);
        model.addAttribute("comcount", comcount);

        return "event-details5";
    }

    @GetMapping("/noplay")
    public String noplay() {
        return "noplay";
    }

    @GetMapping("/todetails4")
    public String todetails4(Model model, HttpSession session) {
        User user = (User) session.getAttribute("loginuser");
        if (user.getCpid() == 0) {
            return "redirect:/noplay";
        } else {
            Competition competition = comService.selectComByid(user.getCpid());

            model.addAttribute("coms", competition);

            return "event-details4";
        }
    }


    @GetMapping("/event-operation1")
    public String eventoperation1(Model model) {
        List<Competition> coms = comService.findAllComs();

        int comcount = comService.allComsaccount();


        model.addAttribute("coms", coms);
        model.addAttribute("comcount", comcount);

        return "event-operation1";
    }


    @GetMapping("/del-event")
    public String delevent(@RequestParam int id) {
        int i = comService.delcomp(id);

        return "redirect:/event-operation1";
    }

    @GetMapping("/toadd-event")
    public String toaddevent(Model model) {
        List<Place> placeList = placeService.findAllPlaces();
        model.addAttribute("places", placeList);

        return "toadd-event";

    }

    @PostMapping("/doadd-event")
    public String doaddevent(Competition competition) {

        int i = comService.addcomp(competition);

        return "redirect:/event-operation1";
    }


    @GetMapping("/toupdate-event")
    public String toupdateevent(Model model, HttpSession session) {
        User user = (User) session.getAttribute("loginuser");
        if (user.getCpid() == 0) {
            return "redirect:/noplay";
        } else {


            Competition com = comService.selectComByid(user.getCpid());

            List<Place> placeList = placeService.findAllPlaces();
            model.addAttribute("places", placeList);
            model.addAttribute("com", com);

            return "toupdate-event";
        }

    }

    @PostMapping("/doupdate-event")
    public String doupdateevent(Competition competition) {


        int i = comService.updatecomp(competition);

        return "redirect:/todetails2";
    }


}
